#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/23 11:38
    Desc  :
--------------------------------------
"""
import random

import requests

from application import app
from common.libs.UrlParseUtils import UrlParseUtils


class ResultRecordUtils:
    """
    获取结果
    """

    def __init__(self, organizeId):
        # 机构id
        self.organizeId = organizeId
        self.baseUrl = app.config["CHILDFITURL"]
        print(self.baseUrl)
        self.url = f'{self.baseUrl}/application/gpa?organizeId={organizeId}&&xx={random.randint(1, 99999)}'
        self.titleList = []
        # id
        self.evaluationRecordId = 0
        # 最后更新时间
        self.lastUpdateTime = 1
        # 宝宝姓名
        self.childName = 2
        # 宝宝月龄
        self.childMonthAge = 4
        # 状态
        self.status = 5
        # 剪纸正方形
        self.square = 11
        self.title = [
            "ID",
            "最后更新时间",
            "宝宝姓名",
            "宝宝性别",
            "宝宝月龄",
            "状态",
            "投币左手",
            "投币右手",
            "穿珠",
            "画隧道",
            "剪纸圆形",
            "剪纸正方形",
            "坐位体前屈",
            "双手接沙包",
            "投掷沙包",
            "双手接球",
            "同侧跳",
            "原地单脚跳",
            "双脚站立直线",
            "睁眼单腿站左",
            "睁眼单腿站右",
            "闭眼单腿站左",
            "闭眼单腿站右",
            "踮脚走",
            "十米折返跑",
            "六边形跳",
            "双脚左右跳",
            "握力左",
            "握力右",
            "立定跳远",
            "两臂支撑",
            "仰卧起坐",
            "俯卧背伸"
        ]

    def get_result(self):
        res = requests.get(url = self.url)
        resultList = res.json()
        if isinstance(resultList, list) and len(resultList) > 0:
            # self.title = resultList[0]
            for i in resultList:
                if i[self.lastUpdateTime] == '最后更新时间':
                    self.titleList.append(i)
                    resultList.remove(i)
            return resultList
        return []

    def get_name_list(self):
        """
        获取用户姓名列表
        :return:
        """
        name_list = []
        resultList = self.get_result()
        if len(resultList) > 0:
            for i in resultList:
                id = resultList.index(i)
                name_list.append({'id': id, 'value': i[self.childName], 'label': i[self.childName]})

        return name_list

    def set_age_index(self, resultList: list):
        """
        判断是否为3岁
        :return:
        """
        if '3岁' == resultList[self.childMonthAge][:2]:
            resultList = resultList.insert(self.square, '无')
            return resultList
        return None

    def get_result_by_name(self, resultList: list, result, res: list, name: str):

        for i in resultList:
            if name in i[self.childName]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_time(self, resultList: list, result, res: list, time: str):
        for i in resultList:
            if time == i[self.lastUpdateTime][:10]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_name_time(self, resultList: list, result, res: list, name: str, time: str):
        for i in resultList:
            if time == i[self.lastUpdateTime][:10] and name in i[self.childName]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_status(self, resultList: list, result, res: list, status: str):
        for i in resultList:
            if status == i[self.status]:
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_name_status(self, resultList: list, result, res: list, name: str, status: str):

        for i in resultList:
            if status == i[self.status] and name in i[self.childName]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_name_time_status(self, resultList: list, result, res: list, name: str, time: str, status: str):

        for i in resultList:
            if status == i[self.status] and name in i[self.childName] and time == i[self.lastUpdateTime][:10]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_time_status(self, resultList: list, result, res: list, time: str, status: str):

        for i in resultList:
            if status == i[self.status] and time in i[self.lastUpdateTime][:10]:
                n = self.set_age_index(i)
                if n:
                    result.append(n)
                result.append(i)
        for i in result:
            res.append(dict(zip(self.title, i)))
        return res

    def get_result_by_filter(self, **kwargs):
        resultList = self.get_result()
        result = []
        res = []
        name, time, status = kwargs.get('name'), kwargs.get('time'), kwargs.get('status')
        # self.title = self.titleList[1]
        if isinstance(resultList, list) and len(resultList) > 0:
            if name and time is None and status is None:
                return self.get_result_by_name(resultList, result, res, name)
            elif name is None and time and status is None:
                return self.get_result_by_time(resultList, result, res, time)
            elif name and time and status is None:
                return self.get_result_by_name_time(resultList, result, res, name, time)
            elif status and time is None and name is None:
                return self.get_result_by_status(resultList, result, res, status)
            elif status and time is None and name:
                return self.get_result_by_name_status(resultList, result, res, name, status)
            elif status and time and name is None:
                return self.get_result_by_time_status(resultList, result, res, time, status)
            elif status and name and time:
                return self.get_result_by_name_time_status(resultList, result, res, name, time, status)
            else:
                for i in resultList:
                    res.append(dict(zip(self.title, i)))
                return res
        return res


class GPARecordUtils:
    """
    GPA扫描结果类
    """
    def __init__(self):
        """
        """
        self.baseUrl = app.config["CHILDFITURL"]

    def get_record_detail(self, evaluationRecordId):
        """
        获取测查详情
        :return:
        """
        url = f'{self.baseUrl}/application/gpaInfo?evaluationRecordId={evaluationRecordId}'

        res = requests.get(url = url)

        return res.json()

    def set_record_detail(self, evaluationRecordId, result):
        url = f'{self.baseUrl}/application/gpaEdit'
        data = {
            'evaluationRecordId': evaluationRecordId,
            'resultJsonString': result
        }

        headers = {
            # 'Content-Type': 'application/x-www-form-urlencoded'
        }
        res = requests.post(url = url, data = data, headers = headers)
        # res = requests.post(url = url, headers = headers)
        app.logger.info(res.text)
        app.logger.info(res.url)
        return res.text


class ChildFitData:
    """
    礼橙贝超级后台相关类
    """
    def __init__(self):
        self.base_url = app.config['DATACHILDFITURL']
        self.token = None

        self.headers = {
            'accesstoken': None
        }

    def get_token(self):
        """
        获取token
        :return:
        """

        path = '/admin/admin/login'
        url = self.base_url + path

        data = {
            'username': 'song123456',
            'password': 'E10ADC3949BA59ABBE56E057F20F883E'
        }

        res = requests.post(url = url, data = data)
        self.token = res.json()['data']['accesstoken']

    def get_organize_list(self):
        """
        获取机构列表
        :return:
        """
        path = '/admin/organize/list'
        url = self.base_url + path
        self.headers['accesstoken'] = self.token
        data = {
            'page': 1,
            'size': 2147483647
        }

        res = requests.post(url = url, data = data, headers = self.headers)

        return res.json()['data']['array']


if __name__ == '__main__':
    result = ResultRecordUtils(organizeId = 434)

    # print(result.get_result_by_filter(time = '2020-11-20'))
    print(len(result.get_result_by_filter(name = '陈')))
